<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教师管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    
    <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item label="选择学校" prop="roleId">
          <el-select size="small" v-model="editForm.cou_datail" placeholder="请选择" class="userRole">
            <el-option label="石河子大学" value="1"></el-option>
            <el-option label="塔里木大学" value="2"></el-option>
          </el-select>
        </el-form-item>
      <!-- <el-form-item label="">
        <el-input size="small" v-model="formInline.userName" placeholder="输入教师名"></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
      element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column sortable prop="id" label="序号" width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_title" label="课程名" width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_start_time" label="课程开始时间" min-width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_end_time" label="课程结束时间" min-width="100">
      </el-table-column>
      <el-table-column sortable prop="tea_name" label="授课教师" width="100">
      </el-table-column>
      <el-table-column sortable prop="cou_datail" label="课程简介" width="100">
      </el-table-column>
      <el-table-column sortable prop="number" label="签到人数"  width="100">
      </el-table-column>
      <el-table-column label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" @click="signIn(scope.$index, scope.row)">签到</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog('edit')">
      <el-form label-width="120px" ref="editForm" :model="editForm" :rules="rules">
          <el-form-item label="学校" prop="roleId">
          <el-select size="small" v-model="editForm.cou_datail" placeholder="请选择" class="userRole">
            <el-option label="石河子大学" value="1"></el-option>
            <el-option label="塔里木大学" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名" prop="cou_title">
          <el-input size="small" v-model="editForm.cou_title" auto-complete="off" placeholder="请输入课程名称"></el-input>
        </el-form-item>
        <el-form-item label="课程开始时间" prop="cou_start_time">
          <el-input size="small" v-model="editForm.cou_start_time" auto-complete="off" placeholder="请选择课程开始时间"></el-input>
        </el-form-item>
        <el-form-item label="课程结束时间" prop="cou_end_time">
          <el-input size="small" v-model="editForm.cou_end_time" placeholder="请选择课程结束时间"></el-input>
        </el-form-item>
        <el-form-item label="课程简介" prop="cou_datail">
          <el-input size="small" v-model="editForm.cou_datail" placeholder="请输入课程简介"></el-input>
        </el-form-item>
        
        <!-- <el-form-item label="课程情况" prop="roleId">
          <el-select size="small" v-model="editForm.cou_datail" placeholder="请选择" class="userRole">
            <el-option label="已上课" value="1"></el-option>
            <el-option label="未上课" value="2"></el-option>
          </el-select>
        </el-form-item> -->
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 导入请求方法
  import {
    userList,
    userInsert,
    userDelete,
    userUpdate,
    userSearch
  } from "@/api/user";
 
  import Pagination from "@/components/Pagination";
  export default {
    data() {
      return {
        loading: false, //是否显示加载
        title: "添加",
        editFormVisible: false, //控制编辑页面显示与隐藏
        // 编辑与添加form表单
        editForm: {
          id: 0,
          cou_title: null,
          cou_start_time:null,
          cou_end_time:null,
          c_time: null,
          tea_name: null,
          cou_datail: null,
          number: 0,
        },
        editForm_1: {
          id: 0,
          cou_title: null,
          cou_start_time:null,
          cou_end_time:null,
          tea_name: null,
          cou_datail: null,
          number: 0,
        },
        // 角色对象
        role: {
          roleId: "",
          roleName: "",
        },
        // rules表单验证
        rules: {
          cou_title: [{
            required: true,
            message: "请输入课程名",
            trigger: "blur",
          }, ],
          cou_start_time: [{
            required: true,
            message: "请输入课程开始时间",
            trigger: "blur",
          }, ],
          cou_end_time: [{
            required: true,
            message: "请输入课程结束时间",
            trigger: "blur",
          }, ],
        },
        // 搜索用参数
        formInline: {
          pageNum: 1,
          pageSize: 10,
          userName: "",
        },
        //用户列表数据
        userData: [],
        defaultProps: {
          children: "children",
          label: "name",
        },
        // 选中
        checkmenu: [],
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 5,
          total: 3,
        },
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 数据发生改变
     */
    watch: {},

    /**
     * 使用api获得userList
     */
    mounted() {
      this.getdata(this.formInline);
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取数据方法
      getdata(parameter) {
        this.loading = true
        // 模拟数据开始 
        let res = {
          code: 0,
          msg: null,
          count: 12,
          data: [{
              id: '1',
              cou_title: 'java',
              cou_start_time:'2022-06-13 17:18:06',
              cou_end_time:'2022-06-13 19:18:06',
              tea_name: '赵老师',
              cou_datail: '已上课',
              number: '10',
            },
            {
              id: '2',
              cou_title: 'css',
              c_time: '2022-06-13 17:18:06',
              tea_name: '钱老师',
              cou_datail: '已上课',
              number: '15',
            },
            {
              id: '3',
              cou_title: 'html',
              c_time: '2022-06-13 17:18:06',
              tea_name: '孙老师',
              cou_datail: '已上课',
              number: '20',
            },
            {
              id: '4',
              cou_title: 'JavaScript',
              c_time: '2022-06-13 17:18:06',
              tea_name: '李老师',
              cou_datail: '已上课',
              number: '25',
            },
            {
              id: '5',
              cou_title: 'springboot',
              c_time: '2022-06-13 17:18:06',
              tea_name: '周老师',
              cou_datail: '已上课',
              number: '40',
            },
            {
              id: '6',
              cou_title: 'javaWeb',
              c_time: '2022-06-13 17:18:06',
              tea_name: '吴老师',
              cou_datail: '已上课',
              number: '10',
            },
            {
              id: '7',
              cou_title: 'admin',
              c_time: '2022-06-13 17:18:06',
              tea_name: '郑老师',
              cou_datail: '已上课',
              number: '30',
            },
            {
              id: '8',
              cou_title: 'admin',
              c_time: '2022-06-13 17:18:06',
              tea_name: '王老师',
              cou_datail: '已上课',
              number: '20',
            },
            {
              id: '9',
              cou_title: 'admin',
              c_time: '2022-06-13 17:18:06',
              tea_name: '赵老师',
              cou_datail: '已上课',
              number: '25',
            },
            {
              id: '10',
              cou_title: 'admin',
              c_time: '2022-06-13 17:18:06',
              tea_name: '赵老师',
              cou_datail: '已上课',
              number: '34',
            }
          ]
        }
        this.loading = false
        this.userData = res.data
        // 分页赋值
        this.pageparm.currentPage = this.formInline.page
        this.pageparm.pageSize = this.formInline.limit
        this.pageparm.total = res.count
      // 获取数据方法
    //   getdata(parameter) {
    //     this.loading = true;
    //     // 获取用户列表
    //     userSearch(parameter).then((res) => {
    //       this.loading = false;
    //       if (res.code != 200) {
    //         this.$message({
    //           type: "error",
    //           message: res.message,
    //         });
    //       } else {
    //         this.userData = res.data.list;
    //         // 分页赋值
    //         this.pageparm.currentPage = this.formInline.pageNum;
    //         this.pageparm.pageSize = this.formInline.pageSize;
    //         this.pageparm.total = res.data.total;
    //       }
    //    });
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.pageNum = parm.currentPage;
        this.formInline.pageSize = parm.pageSize;
        this.getdata(this.formInline);
      },
      //搜索事件
      search() {
        this.getdata(this.formInline);
        userSearch(parameter).then((res) => {
      
      if (res.code != 200) {
            this.$message({
              type: "error",
              message: res.message,
            });
          } else {
            this.userData = res.data.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.data.total;
          }
        })
    
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val;
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true;
        if (row != undefined && row != "undefined") {
          this.title = "修改用户";
          this.editForm.userId = row.userId;
          this.editForm.userName = row.userName;
          this.editForm.password = row.password;
          this.editForm.roleId = row.roleId;
          this.editForm.roleName = row.roleName;
          this.editForm.email = row.email;
        } else {
          this.title = "添加用户";
          this.editForm.userId = '';
          this.editForm.userName = '';
          this.editForm.password = '';
          this.editForm.roleId = '';
          this.editForm.roleName = null;
          this.editForm.email = '';
        }
      },

      // 编辑、添加提交方法
      submitForm(editData) {
        this.$refs[editData].validate((valid) => {
          //判断userId决定调用userInsert还是userUpdate
          if (valid) {
            console.log(this.editForm_1);
            this.editForm_1.userName = this.editForm.userName;
            this.editForm_1.password = this.editForm.password;
            this.editForm_1.roleId = this.editForm.roleId;
            // this.editForm.roleName = null;
            this.editForm_1.email = this.editForm.email;
            if (this.editForm.userId == 0) {
              //insert
              userInsert(this.editForm_1).then((res) => {
                if (res.code != 200) {
                  this.$message({
                    type: "error",
                    message: res.message,
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "⽤户添加成功！",
                  })
                  this.getdata(this.formInline); //重新获取⽤户列表
                  this.editFormVisible = false; //隐藏表单
                }
              });
            } else {
              //update
              userUpdate(this.editForm).then((res) => {
                if (res.code != 200) {
                  this.$message({
                    type: "error",
                    message: res.message,
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "⽤户修改成功！",
                  });
                  this.getdata(this.formInline); //重新获取⽤⼾列表
                  this.editFormVisible = false; //隐藏表单
                }
              });
            }
          } else {
            //验证错误！
            this.$message({
              type: "info",
              message: "验证错误！",
            });
          }
        });
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == "edit") {
          this.editFormVisible = false;
        }
      },
      //签到
      signIn: function (index, row) {
        this.editFormVisible = true;
        //TODO
      },
      // 删除用户
      deleteUser(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //调用userDelete删除用户信息
          let userId = row.userId;
          userDelete({
            userId
          }).then(res => {
            if (res.code != 200) {
              this.$message({
                type: 'error',
                message: res.message
              })
            } else {
              this.$message({
                type: 'success',
                message: '⽤户删除成功！'
              })
              this.getData(this.formInline) //重新获取⽤⼾列表
            }
          })
        })
      },
    }
  };
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>